
/* Create Date: yy/MM/dd
 * 
 * Modify Record:
 *  <date>      <author>       <version>       <desc>
 * -------------------------------------------------------------------
 *  2012/03/09    hang.qi      2.0             Create the file.
 */

/**
 * @file    
 * @ingroup 
 * @brief   
 *
 *  
 *
 * @author  hang.qi(qihang@sgchip.com)
 * @date    2012/05/01
 * @version 2.0
 */ 

#ifndef UART_H
#define UART_H


/* If you want to test sending and recving function of all the uart, please define a Macro named "UART_LOOP" below */
#undef 							UART_LOOP
//#define 							UART_LOOP	
//#define							_ARM_DAEMON_
#define UART_NUM				5
#define UART_START				0
 #define DEBUG
#define UART0_DEV		   		"/dev/ttyS1"		
#define UART1_DEV		   		"/dev/ttyS2"
#define UART2_DEV		   		"/dev/ttyS3"
#define UART3_DEV		  		"/dev/ttyS4"
#define UART4_DEV		   		"/dev/ttyS5"
#define UART5_DEV		   		"/dev/ttyS6"


#define BUFFER_LEN				256
#define UART_RBUFF_LEN			BUFFER_LEN
#define UART_DELAY				1000


#define NONE_PARITY				'N'
#define EVEN_PARITY				'E'
#define ODD_PARITY				'O'


#define DEFAULT_FD				0xFFFFFFFF
#define TTYS 					0


/*******************************************************************************
 *
 *  Type define
 *
 *******************************************************************************
 */

typedef  unsigned  char   INT08U;	   /*  8-bit unsigned integer  */
typedef  signed    char   INT08S;	   /*  8-bit   signed integer  */
typedef  unsigned  short  INT16U;	   /* 16-bit unsigned integer  */
typedef  signed    short  INT16S;	   /* 16-bit   signed integer  */
typedef  unsigned  long   INT32U;	   /* 32-bit unsigned integer  */
typedef  signed    int    INT32S;	   /* 32-bit   signed integer  */
typedef  float             FP32;	   /* 32-bit floating point*/

typedef  void             VOID;


typedef struct 
{	
	INT32U 			fd;
	INT32U 			baud;        		/* 1200, 2400, ... 115200 */
	INT08U 			data_bit;     		/* 7 or 8 */
	INT08U 			stop_bit ;     		/* 01-1; 00-1.5; 10-2; other reverse*/
	INT08U 			parity_bit;		/* 'N', 'E' or 'O' */
	INT08U 			valid;			/* 1, valid; 0, invalid */
	INT16U 			com_num;  		/* com1, com2, ... com6 */
	INT08U			recv_buff[BUFFER_LEN];
	INT32U			recv_len;
	INT08U			send_buff[BUFFER_LEN];
	INT32U			send_len;
 }arm_uart_para_info_t;


/***************************** UART BAUD ********************************/

#define BAUD_115200				115200
#define BAUD_38400  				38400
#define BAUD_19200  				19200
#define BAUD_9600    				9600
#define BAUD_4800    				4800
#define BAUD_2400    				2400
#define BAUD_1200    				1200

/************************************************************************/



/****************************** DATA BIT ********************************/
typedef enum
{
	DATA_BIT_7 = 				7,
	DATA_BIT_8,
        INVALID_DATA_BIT=		-1
}DATA_BIT;
/************************************************************************/

/***************************** STOP BIT *********************************/
typedef enum
{
	STOP_BIT_1= 					1,
	STOP_BIT_2,
       INVALID_STOP_BIT=				-1
}STOP_BIT;
/************************************************************************/

/****************************** COM NUMBER ******************************/
typedef enum
{
	COM_ZERO =					0,
	COM_ONE,
	COM_TWO,
	COM_THREE,
	COM_FOUR,
	COM_FIVE,
	COM_SIX,
       INVALID_COM =					0xFFFF
}COM_NUM;
/************************************************************************/

/**************************** VALID BIT *********************************/

typedef enum
{
	INVALID =					0,
        VALID
}VALID_BIT;

typedef enum
{
	EMPTY =						0,
        FULL
}BUFFER_STATUS;

typedef enum
{
        UART_EXE_OKAY =			0,
        UART_EXE_FAILE,
        UART_EXE_BAUD_FAILE,
        UART_EXE_PARITY_FAILE,
        UART_EXE_DATABIT_FAILE,
        UART_EXE_STOPBIT_FAILE
}UART_RET;


/************************************************************************/


/************************* SEND QUEUE STATUS ****************************/


#define	get_tty_num(com, dev) 					\
	switch(com)						\
	{							\
		case 0: strcpy(dev,"/dev/ttyS1"); break;	\
		case 1: strcpy(dev,"/dev/ttyS2"); break;	\
		case 2: strcpy(dev,"/dev/ttyS3"); break;	\
		case 3: strcpy(dev,"/dev/ttyS4"); break;	\
		case 4: strcpy(dev,"/dev/ttyS5"); break;	\
		case 5: strcpy(dev,"/dev/ttyS6"); break;	\
		default: strcpy(dev,"/dev/ttyS1"); break;	\
	}



#define OPEN_DEV(dev)						\
	open(dev,O_RDWR|O_NOCTTY|O_NONBLOCK);



#define CLOSE_DEV(fd) 						\
	close(fd);						\
	tcflush(fd, TCOFLUSH);					\
	prINT32Uf("Com%d closeed\n", uart_para_info.com_num);	
	
#define GET_MAX_FD(c, max, i)\
	      for(i = 0; i < UART_NUM; i++)\
			      if(c[i].fd > max)\
				      max = c[i].fd	

extern ssize_t uart_recvn(int fd, void *vptr, size_t n);
extern ssize_t uart_sendn(int fd, const void *vptr, size_t n);
//extern ssize_t uart_recv(int fd, void *ptr, size_t nbytes);
extern ssize_t uart_recv(int fd, INT08U*ptr, size_t nbytes);
extern ssize_t uart_send(int fd, const void *ptr, size_t nbytes);
extern INT32U uart_packet_send(int fd, void*ptr, size_t nbytes);
extern INT32U uart_packet_recv(int fd, void*ptr, size_t nbytes);

//extern VOID *uart_send_task(VOID);
//extern VOID *uart_recv_task(VOID);

#endif
 
